<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/11/20
 * Time: 10:14
 */

namespace App\Services\Api;
use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Models\Admin\ComOptionModel;
use App\Models\Admin\CompanyModel;
use App\Models\CompanysModel;
use App\Models\User\UserOptionsGongchangModel;
use Illuminate\Http\Request;
/// App\Services\Api\Company_options@
class Company_options extends ApiBase
{

    /**
     * 获取工厂成本表
     */
    public function getcostlist(Request $request)
    {
        $type=$request->post('type');
        if($type!=='gc'){
            OA::Json(1,'参数错误');
        }
        $uid=OA::$user->getId();
        /*
        $usersCompany=ComOptionModel::where([
            'uid'=>$uid,
            'type'=>CompanyModel::type_gongchang
        ])->first();*/
        $usersCompany=UserOptionsGongchangModel::where([
            'uid'=>$uid
        ])->first();
        if(!$usersCompany){
            $usersCompany=new UserOptionsGongchangModel;
            $usersCompany->uid=$uid;
            $usersCompany->costlist='';
            $usersCompany->save();
        }
        OA::Json(0,[
            'id'=>$usersCompany->id,
            'costlist'=>$usersCompany->costlist
        ]);
    }


    /**
     * 保存工厂成本表
     */
    public function setcostlist(Request $request)
    {
        $id=$request->post('id');
        $costlist=$request->post('costlist');
        if($id==null||$costlist==null){
            OA::Json(1,'参数错误');
        }
        $uid=OA::$user->getId();
        $usersCompany=UserOptionsGongchangModel::where([
            'id'=>$id
        ])->first();
        if(!$usersCompany){
            OA::Json(1,'参数错误,数据未找到');
        }
        $usersCompany->costlist=$costlist;
        if($usersCompany->save()){
            OA::Json(0,'ok');
        }else{
            OA::Json(1,'参数错误,数据保存失败');
        }
    }



}